package com.example.bitmaptest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.BitmapFactory;
import android.graphics.ImageDecoder;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "htz_test";
    private ImageView imageView;
    private Button next;
    private List<String> imageList;

    private int currentImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.image);
        next = findViewById(R.id.next);

        //获取系统中某个文件夹中所有的图片
        String path = Environment.getExternalStorageDirectory().getPath() + "/test" ;
        Log.e(TAG, "onCreate: " + path  );
        getImageList(path);

        imageView.setImageBitmap(BitmapFactory.decodeFile(imageList.get(0)));
        next.setOnClickListener(this);
    }

    private  void getImageList(String path) {
        File file = new File(path);
        File[] files = file.listFiles();
        imageList = new ArrayList<>();
        Log.e(TAG, "getImageList: " + files.length );

        for (int i = 0; i < files.length; i++) {
            imageList.add(files[i].getAbsolutePath());
        }
    }

    public  boolean checkIsImageFile(String fName) {
        boolean isImageFile = false;
        //获取拓展名
        String fileEnd = fName.substring(fName.lastIndexOf(".") + 1,
                fName.length()).toLowerCase();
        if (fileEnd.equals("jpg") || fileEnd.equals("png") || fileEnd.equals("gif")
                || fileEnd.equals("jpeg") || fileEnd.equals("bmp")) {
            isImageFile = true;

        } else {
            isImageFile = false;
        }
        return isImageFile;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.next:
                if (++currentImage >= imageList.size()){
                    currentImage = 0;
                }
                imageView.setImageBitmap(BitmapFactory.decodeFile(imageList.get(currentImage)));
                break;
        }
    }

    /**
     * ImageDecoder 可以解码不完整或者错误的图片,图过希望显示ImageDecoder解码出错之前的部分图片,可以通过设置setOnPartialImageListener
     * @param id
     * @param path
     * @return
     */
    public static Drawable getDrawable(int id,String path){
        Drawable drawable = null;
        ImageDecoder.Source source = ImageDecoder.createSource(new File(path));
        try {
//            drawable = ImageDecoder.decodeDrawable(source);
            drawable = ImageDecoder.decodeDrawable(source, new ImageDecoder.OnHeaderDecodedListener() {
                @Override
                public void onHeaderDecoded(@NonNull ImageDecoder decoder, @NonNull ImageDecoder.ImageInfo info, @NonNull ImageDecoder.Source source) {
                    //info中包含的是被解码的图片信息
                    //设置图片解码之后的缩放大小
                    decoder.setTargetSize(600,500);
                    decoder.setOnPartialImageListener(new ImageDecoder.OnPartialImageListener() {
                        @Override
                        public boolean onPartialImage(@NonNull ImageDecoder.DecodeException exception) {
                            return true;   //表示即使不能完整的解码全部图片也返回drawable/bitmap
                        }
                    });
                }
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return drawable;
    }
}